<template>
  <div>
    <el-dialog
      v-model="createVisible"
      :title="operation == 'create' ? '添加角色' : '编辑角色'"
      @before-ok="createOk"
      @close="close(createRef)"
      width="600px"
      top="100px"
    >
      <div style="width: 450px">
        <el-form
          :model="createForm"
          ref="createRef"
          :rules="createRules"
          label-width="120px"
          size="default"
          v-loading="createLoading"
        >
          <el-form-item label="角色名称：" prop="role_name">
            <el-input
              v-model="createForm.role_name"
              placeholder="请输入角色名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注：" prop="remarks">
            <el-input
              v-model="createForm.remarks"
              placeholder="备注"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="flex center">
          <el-button @click="createVisible = false">取消</el-button>
          <el-button
            type="primary"
            :loading="btnLoading"
            :disabled="btnLoading"
            @click="createOk(createRef)"
            >确定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
export default {
    name:'systemRoleCreate'
}
</script>

<script lang="ts" setup>
import { ref, getCurrentInstance, nextTick, reactive } from "vue";
import { useConfig } from "@/store";
import type { CommonProps, FormInstance, FormRules } from "element-plus";
import { create, update, getDetail } from "@/api/system/role";
import type { Result, ResultError } from "@/types";
const {
  proxy: { $utils },
} = getCurrentInstance() as any;

const emit = defineEmits(["success"]);

const operation = ref("create");

const operationId = ref(0);

const createVisible = ref(false);
const createRef: any = ref<typeof FormInstance>();

const createForm = ref({
  role_name: "",
  remarks: "",
});

const createRules: any = reactive({
  role_name: [{ required: true, message: "角色名称不能为空！" }],
});

const createLoading: any = ref(false);

const toInit = () => {
  if (!operationId.value) {
    return;
  }
  getDetail({ id: operationId.value })
    .then((res: Result) => {
      createForm.value.role_name = res.data.role_name;
      createForm.value.remarks = res.data.remarks;
    })
    .catch((err: ResultError) => {
      $utils.errorMsg(err);
    });
};

const btnLoading = ref(false);

const createOk = async (formEl: typeof FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid: any, fields: any) => {
    if (valid) {
      if (btnLoading.value) {
        return;
      }
      btnLoading.value = true;
      let operationApi: any = null;
      if (operation.value == "create") {
        operationApi = create(createForm.value);
      } else {
        operationApi = update(
          Object.assign(
            {
              id: operationId.value,
            },
            createForm.value
          )
        );
      }
      operationApi
        .then((res: Result) => {
          $utils.successMsg(res.message);
          close(formEl);
          emit("success");
          btnLoading.value = false;
        })
        .catch((err: ResultError) => {
          $utils.errorMsg(err);
          btnLoading.value = false;
        });
    }
  });
};

const open = (id = 0) => {
  if (id != 0) {
    operation.value = "update";
    operationId.value = id;
  } else {
    operation.value = "create";
  }
  nextTick(() => {
    toInit();
  });
  createVisible.value = true;
};

const close = async (formEl: typeof FormInstance | undefined) => {
  if (formEl) {
    formEl.resetFields();
  }
  operationId.value = 0;
  createVisible.value = false;
};

defineExpose({ open });
</script>